package com.BlogPark.controller;

import com.BlogPark.controller.exception.Code;
import com.BlogPark.controller.result.Result;
import com.BlogPark.pojo.Feedback;
import com.BlogPark.service.FeedbackService.FeedbackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Auther: 业峰
 * @Date: 2022/8/10 - 08 - 10 - 22:10
 * @version: 1.0
 * 用户反馈 包括：1.添加反馈、2.按更新时间查询反馈、3.删除反馈、4.查询某一用户的所有反馈
 */
@RestController
@RequestMapping("/feedback")
public class FeedbackController {

    @Autowired
    private FeedbackService feedbackService;

    /**
     * 添加反馈信息
     * @param feedback
     * @return
     */
    @PostMapping("/addFeedback")
    public Result addFeedback(@RequestBody Feedback feedback){
        Integer code;
        String msg;
        //设置文章的添加时间
        long currentTime = System.currentTimeMillis();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
        Date date = new Date(currentTime);
        feedback.setDate(date);

        try {
            int row = feedbackService.addFeedback(feedback);
            if (row == 1){
                //添加成功
                code = Code.SAVE_OK;
                msg = "反馈提交成功";
            }else {
                code = Code.SAVE_ERR;
                msg = "反馈提交失败";
            }
        }catch (Exception e){
            code = Code.SAVE_ERR;
            msg = "反馈提交失败";
            return new Result(code,null,msg);
        }
        return new Result(code,null,msg);
    }

    /**
     * 按更新时间查询反馈
     * @return 返回反馈列表
     */
    @GetMapping("/showFeedback")
    public Result showFeedback(){
        Integer code;
        String msg;
        List<Feedback> feedbacks = feedbackService.showFeedback();
        if (feedbacks != null || !feedbacks.isEmpty()){
            code = Code.GET_OK;
            msg = "查询成功";
        }else {
            code = Code.GET_ERR;
            msg = "查询失败";
        }
        return new Result(code,feedbacks,msg);
    }

    /**
     * 根据反馈的id删除对应的反馈
     * @param id
     * @return
     */
    @DeleteMapping("/deleteFeedback/{id}")
    public Result deleteFeedback(@PathVariable("id") Integer id){
        Integer code;
        String msg;
        int row = feedbackService.deleteFeedback(id);
        if (row == 1){
            code = Code.DELETE_OK;
            msg = "反馈删除成功";
        }else {
            code = Code.DELETE_ERR;
            msg = "反馈删除失败";
        }
        return  new Result(code,null,msg);
    }

    /**
     *查询某一用户的所有反馈
     * @param userId
     * @return
     */
    @GetMapping("/showFeedbackByUser")
    public Result showFeedbackByUser(@RequestParam("userId") Integer userId){
        Integer code;
        String msg;
        List<Feedback> feedbacks = feedbackService.showFeedbackByUserId(userId);
        if (feedbacks != null && !feedbacks.isEmpty()){
            code = Code.GET_OK;
            msg = "查询成功";
        }else {
            code = Code.GET_ERR;
            msg = "查询失败";
        }
        return new Result(code,feedbacks,msg);
    }
}
